﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PMS.BusinessFacade;
using PMS.EntityManage;
using PMS.Common;
using PMS.EntityQuery;
using System.Data.SqlClient;
using System.Collections.Generic;

public partial class Modules_Personal_ModTargetAdd : System.Web.UI.Page
{
    IPerf_Personal_TargetHandler targetHandler = new Perf_Personal_TargetManage();
    IPerf_Personal_TargetTypeHandler ttHandler = new Perf_Personal_TargetTypeManage();
    IPerf_Personal_ModTargetHandler modTargetHandler = new Perf_Personal_ModTargetManage();
    IPerf_Personal_ModTargetDtlHandler modTargetDtlHandler = new Perf_Personal_ModTargetDtlManage();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!CheckPurviews())
        {
            Response.Redirect("~/Login.aspx");
        }
        if (!IsPostBack)
        {
            dataBind();
        }
    }

    private bool CheckPurviews()
    {
        if (Session["UserPurviews"] != null)
        {
            string strPur = Session["UserPurviews"].ToString();
            if (strPur.Contains("302041"))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }

    private void dataBind()
    {
        FillTargetType();
    }

    private void FillTargetType()
    {
        DataTable dtTargetType = ttHandler.GetPerf_Personal_TargetTypeList(" Status = @Status", new SqlParameter[] { new SqlParameter("@Status", (int)Enums.NormalStatus.Vallid) }, " id asc");
        ddlTargetType.DataTextField = "Name";
        ddlTargetType.DataValueField = "ID";
        ddlTargetType.DataSource = dtTargetType;
        ddlTargetType.DataBind();

        ddlTargetType_SelectedIndexChanged(null,null);
    }

    protected void ddlTargetType_SelectedIndexChanged(object sender, EventArgs e)
    {
        int ttId = int.Parse(ddlTargetType.SelectedValue.ToString());
        DataTable dt = targetHandler.GetPerf_Personal_TargetList(" TargetType_ID = @TargetType_ID ", new SqlParameter[]{ new SqlParameter("@TargetType_ID", ttId)}, " Code asc");

        DataRow row = dt.NewRow();
        row["ID"] = "0";
        row["Name"] = "     ";
        dt.Rows.InsertAt(row, 0);

        ddlCurrent.DataSource = dt;
        ddlCurrent.DataValueField = "ID";
        ddlCurrent.DataTextField = "Name";
        ddlCurrent.DataBind();
    }

    protected void ddlCurrent_SelectedIndexChanged(object sender, EventArgs e)
    {
        int selectedId = int.Parse(ddlCurrent.SelectedValue.ToString());
        if (selectedId > 0)
        {
            Perf_Personal_TargetQuery targetQuery = targetHandler.GetPerf_Personal_TargetQuery(selectedId);
            if (targetQuery != null)
            {
                txtCode.Text = targetQuery.Code;
                txtName.Text = targetQuery.Name;
                txtScore.Text = targetQuery.Score.ToString();
                txtRemark.Text = targetQuery.Remark;
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if ("".Equals(txtCode.Text.Trim()))
        {
            Jscript.Alert("代码不能为空", this.Page);
            return;
        }
        if ("".Equals(txtName.Text.Trim()))
        {
            Jscript.Alert("名称不能为空", this.Page);
            return;
        }
        if ("".Equals(txtScore.Text.Trim()))
        {
            Jscript.Alert("分值不能为空", this.Page);
            return;
        }
        //if (Request["ID"] != null && Request["ID"] != string.Empty)
        //{
        //    if (isCodeExists(txtCode.Text.Trim(), int.Parse(Request["ID"])))
        //    {
        //        Jscript.Alert("代码已存在", this.Page);
        //        return;
        //    }
        //}
        //else
        //{
        //    if (isCodeExists(txtCode.Text.Trim()))
        //    {
        //        Jscript.Alert("代码已存在", this.Page);
        //        return;
        //    }
        //}

        int targetId = 0;
        Perf_Personal_TargetQuery query = new Perf_Personal_TargetQuery();
        
        query.TargetOwner_ID = 0;
        query.Code = txtCode.Text.Trim();
        query.Name = txtName.Text.Trim();
        query.TargetType_ID = int.Parse(ddlTargetType.SelectedValue);
        query.Score = float.Parse(txtScore.Text.Trim());
        query.Status = (int)Enums.NormalStatus.Vallid;
        query.IsRequired = (int)Enums.YesNoStatus.No;
        query.User_ID = int.Parse(Session["CurrentUserID"].ToString());
        query.Remark = txtRemark.Text;
        query.Create_Date = DateTime.Now;

        targetHandler.InsertPerf_Personal_Target(query, ref targetId);
        if (targetId != 0)
        {
            int modTargetId = int.Parse(Request["modId"]);

            Perf_Personal_ModTargetQuery modTargetQuery = modTargetHandler.GetPerf_Personal_ModTargetQuery(modTargetId);
            if (modTargetQuery.Status != (int)Enums.PersonalModTargetStatus.NewAssign)
            {
                Jscript.Alert("非新分配状态，不能添加", this.Page);
                return;
            }

            Perf_Personal_ModTargetDtlQuery dtlQuery = new Perf_Personal_ModTargetDtlQuery();
            dtlQuery.ModTarget_ID = modTargetId;
            dtlQuery.Target_ID = targetId;
            dtlQuery.Type = (int)Enums.PersonalModTargetType.NewAdded;
            dtlQuery.Remark = txtRemark.Text;
            modTargetDtlHandler.InsertPerf_Personal_ModTargetDtl(dtlQuery);

            Jscript.Alert("添加成功！", this.Page);
            Jscript.ReturnAndClose(this.Page, "true");
        }
        else
        {
            Jscript.Alert("添加失败", this.Page);
        }
    }


    private bool isCodeExists(string code)
    {
        bool istrue = false;

        DataTable dt = targetHandler.GetPerf_Personal_TargetList(" Code = @Code", new SqlParameter[] { new SqlParameter("@Code", code) });
        if (dt != null && dt.Rows.Count > 0)
        {
            istrue = true;
        }
        return istrue;
    }

    private bool isCodeExists(string code, int postId)
    {
        bool istrue = false;

        DataTable dt = targetHandler.GetPerf_Personal_TargetList("Code=@Code and ID!=@Id", new SqlParameter[] { new SqlParameter("@Code", code), new SqlParameter("@Id", postId.ToString()) });

        if (dt != null && dt.Rows.Count > 0)
        {
            istrue = true;
        }
        return istrue;
    }

}
